package it.unina.lab.anm.ui;

import it.unina.lab.anm.Costants;
import it.unina.lab.anm.InteractionManager;
import it.unina.lab.anm.models.Location;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.Shape;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class GestureLocationOverlay extends Overlay  {

	private ArrayList<PointF> touchPoints = null;
	private ArrayList<GeoPoint> geoTouchPoints = null;
	private Paint drawingPaint = null;
	private Projection projection = null;
	private Path percorso = null;
	private PointF start = null;
	private boolean drawable = true; //permette di disegnare o meno sulla mappa
	private final String TAG = "MyOverlay";
	
	public GestureLocationOverlay(MapView mapView, Context context){
        projection = mapView.getProjection(); 
        drawingPaint = new Paint();
		drawingPaint.setColor(Color.RED);
		drawingPaint.setStrokeWidth(5);
		drawingPaint.setStyle(Paint.Style.STROKE);
		drawingPaint.setAntiAlias(true);
		touchPoints = new ArrayList<PointF>();
		geoTouchPoints = new ArrayList<GeoPoint>();
        
    }
	
	
	public void draw(Canvas canvas, MapView mapView, boolean shadow){
        super.draw(canvas, mapView, shadow);
       
        if(touchPoints.size() > 0) {
        	 percorso = new Path();	
             percorso.moveTo(start.x, start.y);
        	for(PointF point : touchPoints){
        		percorso.lineTo(point.x, point.y);
        	}
        	
        	canvas.drawPath(percorso,drawingPaint);
        }	
        
      }	
	
	
	 @Override
     public boolean onTouchEvent(MotionEvent event, MapView mapView) {   
		 	if(!isDrawable())
		 		return false;
		 	
			int action = event.getAction() & MotionEvent.ACTION_MASK;
			boolean ritorno = true;
			switch(action){	
				case MotionEvent.ACTION_DOWN:
							//logga("ACTION_DOWN_START_POINT", event);
							touchPoints.clear();
							geoTouchPoints.clear();
							start = new PointF(event.getX(), event.getY());
							touchPoints.add(start);
							ritorno = false;
				break;
						
				case MotionEvent.ACTION_UP:
							//logga("ACTION_UP", event);
							if(touchPoints.size() == 1){
								Log.i(TAG, "ACTION_UP 1 point!! - TAP ON PALINA?");
								ritorno = false;
							}	
							else {
								touchPoints.add(new PointF(event.getX(),event.getY()));
								printPoints(touchPoints);
								mapView.invalidate();
							} 	
				break;
						
				case MotionEvent.ACTION_MOVE:
							//logga("ACTION_MOVE", event);
							touchPoints.add(new PointF(event.getX(),event.getY()));
				break;
			} 
	 
		Log.i(TAG, "Ritorno: " + ritorno);	
		return ritorno;	
	 }
	 
	 

	/* private void logga(String motionType, MotionEvent event){
		 GeoPoint p = projection.fromPixels((int) event.getX(),(int) event.getY());
		 Log.i(TAG, String.format(motionType + ": %d  ---- X=%f  Y=%f", 
				 event.getPointerCount(), event.getX(), event.getY()));
		 Log.i(TAG,String.format("%d,%d", p.getLongitudeE6(), p.getLatitudeE6()));
	 } */
	 
	 
	 
	 private void printPoints(List<PointF> points){
		 StringBuffer buffer = new StringBuffer();
		 buffer.append("==== PRINT POINTS ===");
		 for(PointF p : points){
			 GeoPoint geop = projection.fromPixels((int) p.x,(int) p.y);
			 geoTouchPoints.add(geop);
			 buffer.append(geop.getLongitudeE6() + "," + geop.getLatitudeE6() + "\n");
		 }
		 
		 geoTouchPoints.add(projection.fromPixels((int) start.x, (int) start.y)); 
		 Log.i(TAG, buffer.toString());
		 
		 //messaggio all'InteractionManager
		 Location loc = new Location();
		 loc.setPoints(geoTouchPoints);
		 Message msg = Message.obtain(InteractionManager.getInteractionManagerIstance().getInboxHandler(), Costants.EVENT_MESSAGE, loc);
     	 msg.sendToTarget();
	
		} 
	 
	 
	 public void deletePath(){
		 touchPoints.clear();
		 geoTouchPoints.clear();
	 }


	public boolean isDrawable() {
		return drawable;
	}


	public void setDrawable(boolean drawable) {
		this.drawable = drawable;
		
	}


	public ArrayList<GeoPoint> getGeoTouchPoints() {
		return geoTouchPoints;
	}


	public void setGeoTouchPoints(ArrayList<GeoPoint> geoTouchPoints) {
		this.geoTouchPoints = geoTouchPoints;
	}
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	    
}
